<?php
/*! \brief
 * This is WFSimplexml class
 * This is the wrapper class of Simplexml
 * \author William Su
 */
class WFSimplexml
{
    /**
     * @var WF
     */
    protected  $objFramework;
    public function __construct($strPathOrDom = null, $objFramework = null){
        if(null !== $objFramework){
            $this->objFramework = $objFramework;
        }
        if(null !== $strPathOrDom){
            if(true === file_exists($strPathOrDom)){
                $this->loadFile($strPathOrDom);
            }else{
                $this->loadString($strPathOrDom);
            }
        }
    }
    /**
     * @var SimpleXML
     */
    private $objSimpleXML;
    /**
     *\brief Description
     * - This method will create Simplexml object from $strDom
     * - and set to property $this->objSimpleXML
     */
    public function loadString($strDom){
        libxml_use_internal_errors(true);
        $this->objSimpleXML = simplexml_load_string($strDom, 'SimpleXMLElement', LIBXML_NOCDATA);
        if(false === $this->objSimpleXML){
            $strMsg = __CLASS__ . '::' . __FUNCTION__ . '()=>$strDom is not a well-formed XML : ' . $strDom;
            throw new WFDomSimplexmlException($strMsg);
        }
    }
    /**
     *\brief Description
     * - This method will create Simplexml object from $strPathXml
     * - and set to property $this->objSimpleXML
     */
    public function loadFile($fileXml){
        libxml_use_internal_errors(true);
        $this->objSimpleXML = simplexml_load_file($fileXml, "SimpleXMLElement", LIBXML_NOCDATA);
        if(false === $this->objSimpleXML){
            $strMsg = __CLASS__ . '::' . __FUNCTION__ . '()=>$fileXml is not a well-formed XML : ' . $fileXml;
            throw new WFDomSimplexmlException($strMsg);
        }
    }
    /**
     *\brief Description
     * - This method will return assocData from SimpleXML object
     * @return array
     */
    public function toArray(){
        $return = $this->objFramework->getTypeFactory()->getObject()->convertObject2Array($this->objSimpleXML);
        return $return;
    }
}